package com.itheima.controller;

import com.itheima.R;
import com.itheima.dto.PageUserDTO;
import com.itheima.dto.UserDTO;
import com.itheima.service.SysUserService;
import com.itheima.vo.PageInfoVO;
import com.itheima.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sys_user")
@RequiredArgsConstructor
public class UserController {

    private final SysUserService sysUserService;

    /**
     * 分页
     * @param pageUserDTO
     * @return
     */
    @GetMapping("/list")
    public R<PageInfoVO<UserInfoVO>> list(PageUserDTO pageUserDTO){
        PageInfoVO<UserInfoVO> pageInfoVO = sysUserService.list(pageUserDTO);
        return R.success(pageInfoVO);
    }

    /**
     * 管理员添加
     * @param userDTO
     * @return
     */
    @PostMapping("/add")
    public R<?> save(@RequestBody UserDTO userDTO){
        sysUserService.save(userDTO);
        return R.success();
    }

    /**
     * 查询回显
     * @return
     */
    @GetMapping("/rowInfo")
    public R<UserInfoVO> rowInfo(String id){
        UserInfoVO userInfoVO = sysUserService.rowInfo(id);
        return R.success(userInfoVO);
    }

    /**
     * 管理员修改
     * @param userDTO
     * @return
     */
    @PostMapping("/update")
    public R<?> update(@RequestBody UserDTO userDTO){
        sysUserService.update(userDTO);
        return R.success();
    }

    /**
     * 管理员删除
     * @return
     */
    @GetMapping("/del")
    public R<?> deleteById(String id){
        sysUserService.deleteById(id);
        return R.success();
    }
}
